﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace XNA.Common
{
	public static class VertexHelper
	{
		#region METHODS (PUBLIC)
		
		/// <summary>
		/// Returns 6 vertices describing 2 triangles for the rectangle.
		/// Co-ordinates must be given anti-clockwise (BL, BR, TR, TL).
		/// </summary>
		public static Vector3[] CreateRectangleVertices (float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4)
		{
			var vertices = new Vector3[6];

			vertices[0] = new Vector3 (x1, y1, z1);
			vertices[1] = new Vector3 (x2, y2, z2);
			vertices[2] = new Vector3 (x4, y4, z4);

			vertices[3] = new Vector3 (x4, y4, z4);
			vertices[4] = new Vector3 (x2, y2, z2);
			vertices[5] = new Vector3 (x3, y3, z3);

			return vertices;
		}

		/// <summary>
		/// Returns 6 vertices describing 2 triangles for the rectangle.
		/// Co-ordinates must be given anti-clockwise (BL, BR, TR, TL).
		/// </summary>
		public static VertexPositionNormalTexture[] CreateRectangle (Vector3 normal, float xTexScaling, float yTexScaling, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4)
		{
			return CreateRectangle (normal, xTexScaling, yTexScaling, p1.X, p1.Y, p1.Z, p2.X, p2.Y, p2.Z, p3.X, p3.Y, p3.Z, p4.X, p4.Y, p4.Z);
		}

		/// <summary>
		/// Returns 6 vertices describing 2 triangles for the rectangle.
		/// Co-ordinates must be given anti-clockwise (BL, BR, TR, TL).
		/// </summary>
		public static VertexPositionNormalTexture[] CreateRectangle (Vector3 normal, float xTexScaling, float yTexScaling, float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4)
		{
			var positionVertices = CreateRectangleVertices (x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4);
			var vertices = new VertexPositionNormalTexture[6];

			vertices[0] = new VertexPositionNormalTexture (
				positionVertices[0],
				normal,
				new Vector2 (0, 0));
			vertices[1] = new VertexPositionNormalTexture (
				positionVertices[1],
				normal,
				new Vector2 (xTexScaling, 0));
			vertices[2] = new VertexPositionNormalTexture (
				positionVertices[2],
				normal,
				new Vector2 (0, yTexScaling));

			vertices[3] = new VertexPositionNormalTexture (
				positionVertices[3],
				normal,
				new Vector2 (0, yTexScaling));
			vertices[4] = new VertexPositionNormalTexture (
				positionVertices[4],
				normal,
				new Vector2 (xTexScaling, 0));
			vertices[5] = new VertexPositionNormalTexture (
				positionVertices[5],
				normal,
				new Vector2 (xTexScaling, yTexScaling));

			return vertices;
		}

		public static Vector3 CalculateNormal ()
		{
			throw new NotImplementedException ();
		}

		#endregion
	}
}